<template>
  <el-dialog :visible.sync="dialog" title="编辑学员信息" append-to-body width="570px">
    <el-form ref="form" :model="student" :rules="rules" size="small" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="学员姓名:" prop="name">
            <el-input v-model="student.name"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="student.gender">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="0">女</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="生日:" prop="birth">
            <el-date-picker v-model="student.birth" type="date" placeholder="选择日期"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="就读学校:" prop="school">
            <el-input v-model="student.school"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { update } from '@/api/student'
export default {
  props: {
    student: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      dialog: false,
      loading: false,
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const para = Object.assign({}, this.student)
          this.loading = true
          update(para).then(res => {
            this.$notify({
              title: '更新学员信息成功',
              type: 'success',
              duration: 2500
            })
            this.loading = false
            this.dialog = false
            this.$parent.getStudentInfo()
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>
